package com.qike.duola.controller;

import com.google.code.kaptcha.impl.DefaultKaptcha;
import com.qike.duola.domain.DuolaProject;
import com.qike.duola.dto.*;
import com.qike.duola.response.BaseResult;
import com.qike.duola.response.RetResponse;
import com.qike.duola.service.DuolaContractService;
import com.qike.duola.service.DuolaQuoteService;
import com.qike.duola.service.IsysResourceService;
import com.qike.duola.service.IsysUserService;
import com.qike.duola.utils.TokenUtil;
import com.qike.duola.vo.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Api(description = "合同控制器")
@CrossOrigin
public class ContractController {
    private static final Log logger = LogFactory.getLog(Object.class);


    @Autowired
    private TokenUtil tokenUtil;

    @Autowired
    private DefaultKaptcha captchaProducer;
    @Autowired
    private DuolaContractService duolaContractService;
    @Autowired
    private IsysUserService userService;
    @Autowired
    private IsysResourceService isysResourceService;

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;


    /**
     * 添加合同
     *
     * @return
     */
    @PostMapping("/contractAdd")
    @ApiOperation("添加合同")
    @ResponseBody
    public BaseResult contractAdd(@RequestBody ContractAddDto contractAddDto) {
        try {
            duolaContractService.contractAdd(contractAddDto);
            return RetResponse.makeOKRsp();
        } catch (Exception e) {
            logger.error(e.getMessage());
            return RetResponse.makeErrRsp(e.getMessage());
        }
    }

    /**
     * 合同列表
     *
     * @return
     */
    @GetMapping("/contractList")
    @ApiOperation("合同列表")
    @ResponseBody
    @ApiResponses({
            @ApiResponse(code = 200, message = "ok", response = ContractListVo.class)
    })
    public BaseResult contractList(ContractListDto contractListDto) {
        try {
            ContractListVo list = duolaContractService.contractList(contractListDto);
            return RetResponse.makeOKRsp(list);
        } catch (Exception e) {
            logger.error(e.getMessage());
            return RetResponse.makeErrRsp(e.getMessage());
        }
    }

    /**
     * 合同详情
     *
     * @return
     */
    @GetMapping("/contractInfo")
    @ApiOperation("合同详情")
    @ResponseBody
    @ApiResponses({
            @ApiResponse(code = 200, message = "ok", response = ContractInfoVo.class)
    })
    public BaseResult contractInfo(Integer id) {
        try {
            ContractInfoVo vo = duolaContractService.contractInfo(id);
            return RetResponse.makeOKRsp(vo);
        } catch (Exception e) {
            logger.error(e.getMessage());
            return RetResponse.makeErrRsp(e.getMessage());
        }
    }

    /**
     * 转正
     *
     * @return
     */
    @PostMapping("/transFormal")
    @ApiOperation("转正")
    @ResponseBody
    public BaseResult transFormal(@RequestBody TransFormalDto transFormalDto) {
        try {
            duolaContractService.transFormal(transFormalDto);
            return RetResponse.makeOKRsp();
        } catch (Exception e) {
            logger.error(e.getMessage());
            return RetResponse.makeErrRsp(e.getMessage());
        }
    }

    /**
     * 添加补充协议
     *
     * @return
     */
    @PostMapping("/addSupplementaryAgreement")
    @ApiOperation("添加补充协议")
    @ResponseBody
    public BaseResult addSupplementaryAgreement(@RequestBody AddSupplementaryAgreementDto addSupplementaryAgreementDto) {
        try {
            duolaContractService.addSupplementaryAgreement(addSupplementaryAgreementDto);
            return RetResponse.makeOKRsp();
        } catch (Exception e) {
            logger.error(e.getMessage());
            return RetResponse.makeErrRsp(e.getMessage());
        }
    }

    /**
     * 修改补充协议
     *
     * @return
     */
    @PostMapping("/editSupplementaryAgreement")
    @ApiOperation("修改补充协议")
    @ResponseBody
    public BaseResult editSupplementaryAgreement(@RequestBody EditSupplementaryAgreementDto editSupplementaryAgreementDto) {
        try {
            duolaContractService.editSupplementaryAgreement(editSupplementaryAgreementDto);
            return RetResponse.makeOKRsp();
        } catch (Exception e) {
            logger.error(e.getMessage());
            return RetResponse.makeErrRsp(e.getMessage());
        }
    }

    /**
     * 查看补充协议
     *
     * @return
     */
    @GetMapping("/supplementaryAgreementInfo")
    @ApiOperation("查看补充协议")
    @ResponseBody
    public BaseResult supplementaryAgreementInfo(Integer id) {
        try {
            SupplementaryAgreementInfoVo vo = duolaContractService.supplementaryAgreementInfo(id);
            return RetResponse.makeOKRsp(vo);
        } catch (Exception e) {
            logger.error(e.getMessage());
            return RetResponse.makeErrRsp(e.getMessage());
        }
    }


    /**
     * 修改合同
     *
     * @return
     */
    @PostMapping("/contractEdit")
    @ApiOperation("修改合同")
    @ResponseBody
    public BaseResult contractEdit(@RequestBody ContractEditDto contractEditDto) {
        try {
            duolaContractService.contractEdit(contractEditDto);
            return RetResponse.makeOKRsp();
        } catch (Exception e) {
            logger.error(e.getMessage());
            return RetResponse.makeErrRsp(e.getMessage());
        }
    }

    /**
     * 删除合同
     *
     * @return
     */
    @PostMapping("/contractDel")
    @ApiOperation("删除合同")
    @ResponseBody
    public BaseResult contractDel(@RequestBody ContractDelDto contractDelDto) {
        try {
            duolaContractService.contractDel(contractDelDto);
            return RetResponse.makeOKRsp();
        } catch (Exception e) {
            return RetResponse.makeErrRsp(e.getMessage());
        }
    }

    /**
     * 开始供货/停止供货
     *
     * @return
     */
    @PostMapping("/startOrStop")
    @ApiOperation("开始供货/停止供货")
    @ResponseBody
    public BaseResult startOrStop(@RequestBody StartOrStopDto startOrStopDto) {
        try {
            duolaContractService.startOrStop(startOrStopDto);
            return RetResponse.makeOKRsp();
        } catch (Exception e) {
            logger.error(e.getMessage());
            return RetResponse.makeErrRsp(e.getMessage());
        }
    }


    /**
     * 获取某个客户下所有已审批的合同下工程列表
     *
     * @return
     */
    @GetMapping("/GetProjectListByUid")
    @ApiOperation("获取某个客户下所有已审批的合同下工程列表")
    @ResponseBody
    @ApiResponses({
            @ApiResponse(code = 200, message = "ok", response = DuolaProject.class)
    })
    public BaseResult GetProjectListByUid(Integer uid) {
        try {
            List<DuolaProject> vo = duolaContractService.GetProjectListByUid(uid);
            return RetResponse.makeOKRsp(vo);
        } catch (Exception e) {
            logger.error(e.getMessage());
            return RetResponse.makeErrRsp(e.getMessage());
        }
    }


    /**
     * 根据合同id获取已绑定报价单的详细列表
     *
     * @return
     */
    @GetMapping("/getQuoteByContractId")
    @ApiOperation("根据合同id获取已绑定报价单的详细列表")
    @ResponseBody
    @ApiResponses({
            @ApiResponse(code = 200, message = "ok", response = DuolaProject.class)
    })
    public BaseResult getQuoteByContractId(Integer id) {
        try {
            List<PayGoodsListByQuoteIdVo> vo = duolaContractService.getQuoteByContractId(id);
            return RetResponse.makeOKRsp(vo);
        } catch (Exception e) {
            logger.error(e.getMessage());
            return RetResponse.makeErrRsp(e.getMessage());
        }
    }


}
